package com.itlxd.controller;


import com.itlxd.common.ThreadLocalUtils;
import com.itlxd.entity.AddressBook;
import com.itlxd.global.SysConstants;
import com.itlxd.global.SysMessages;
import com.itlxd.result.ResultVO;
import com.itlxd.service.IAddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 地址管理 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-12-25
 */
@Api(tags = {"地址相关的api"})
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private IAddressBookService addressBookService;

    @ApiOperation(value="根据用户Id获取地址列表")
    @GetMapping("/list")
    public ResultVO<List<AddressBook>> queryAddressesByUserId() {
        List<AddressBook> addressBooks = addressBookService.listAddressesByUserId(ThreadLocalUtils.getCurrentId());
        return ResultVO.success(addressBooks, SysMessages.QUERY_SUCCESS);
    }

    @ApiOperation("添加地址信息")
    @PostMapping
    public ResultVO<Integer> addAddressBook(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(ThreadLocalUtils.getCurrentId());
        Integer insertNum = addressBookService.insertAddressBook(addressBook);
        return ResultVO.success(insertNum, SysMessages.INSERT_SUCCESS);
    }

    @ApiOperation("查询地址信息")
    @GetMapping("/{id}")
    public ResultVO<AddressBook> queryAddressBookById(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getOneById(id);
        return ResultVO.success(addressBook, SysMessages.QUERY_SUCCESS);
    }

    @ApiOperation("修改地址信息")
    @PutMapping
    public ResultVO<Integer> updateAddressBook(@RequestBody(required = false) AddressBook addressBook) {
        Integer updateNum = addressBookService.updateAddressBook(addressBook);
        return ResultVO.success(updateNum, SysMessages.UPDATE_SUCCESS);
    }

    @ApiOperation("删除地址信息")
    @DeleteMapping
    public ResultVO<Integer> deleteAddress(@RequestParam Long[] ids) {
        Integer deleteNum = addressBookService.deleteAddressBook(ids);
        return ResultVO.success(deleteNum, SysMessages.DELETE_SUCCESS);
    }

    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public ResultVO<Integer> setDefaultAddress(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(ThreadLocalUtils.getCurrentId());
        Integer updateNum = addressBookService.updateDefaultAddressBook(addressBook);
        return updateNum > 0 ? ResultVO.success(updateNum, SysMessages.UPDATE_SUCCESS)
                : ResultVO.error(updateNum, SysMessages.UPDATE_FAIL);
    }

    @ApiOperation("获取默认地址")
    @GetMapping("/default")
    public ResultVO<AddressBook> getDefaultAddress() {
        AddressBook defaultAddressBook = addressBookService.getDefaultAddressBook(ThreadLocalUtils.getCurrentId());
        return ResultVO.success(defaultAddressBook, SysMessages.QUERY_SUCCESS);
    }

}

